library(dplyr)
library(tidyverse)
library(haven)
library(foreign)
library(stargazer)
library(lfe)

load(file="state_house_analysis.RData")


### Table 5
## uncontestedness, measures of partisan bias based on state legislative elections
state_elections_statehouse$treatment<-scale(state_elections_statehouse$EG.L1)
reg1<-(felm(dem_unc_adv~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg1.1<-(felm(dem_unc_adv~treatment+dem_unc_adv.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$declination2.L1)
reg2<-(felm(dem_unc_adv~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg2.1<-(felm(dem_unc_adv~treatment+dem_unc_adv.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$mean_median.L1)
reg3<-(felm(dem_unc_adv~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg3.1<-(felm(dem_unc_adv~treatment+dem_unc_adv.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$symmetry.L1)
reg4<-(felm(dem_unc_adv~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg4.1<-(felm(dem_unc_adv~treatment+dem_unc_adv.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$factor.L1)
reg5<-(felm(dem_unc_adv~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg5.1<-(felm(dem_unc_adv~treatment+dem_unc_adv.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
stargazer(reg1,reg1.1, reg2, reg2.1,reg3,reg3.1, reg4,reg4.1, reg5, reg5.1, star.cutoffs = c(0.1, 0.05))

### Table A5
### uncontestedness, measures of partisan bias based on presidential elections
state_elections_statehouse$treatment<-scale(state_elections_statehouse$EG_pres)
reg1<-(felm(dem_unc_adv~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg1.1<-(felm(dem_unc_adv~treatment+dem_unc_adv.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$declination2_pres)
reg2<-(felm(dem_unc_adv~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg2.1<-(felm(dem_unc_adv~treatment+dem_unc_adv.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$mean_median_pres)
reg3<-(felm(dem_unc_adv~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg3.1<-(felm(dem_unc_adv~treatment+dem_unc_adv.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$symmetry_pres)
reg4<-(felm(dem_unc_adv~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg4.1<-(felm(dem_unc_adv~treatment+dem_unc_adv.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$factor_pres)
reg5<-(felm(dem_unc_adv~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg5.1<-(felm(dem_unc_adv~treatment+dem_unc_adv.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
stargazer(reg1,reg1.1, reg2, reg2.1,reg3,reg3.1, reg4,reg4.1, reg5, reg5.1, star.cutoffs = c(0.1, 0.05))


### Table 6
## candidate quality, measures of partisan bias based on state legislative elections
state_elections_statehouse$treatment<-scale(state_elections_statehouse$EG.L1)
reg1<-(felm(qual_D_adv~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg1.1<-(felm(qual_D_adv~treatment+qual_D_adv.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$declination2.L1)
reg2<-(felm(qual_D_adv~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg2.1<-(felm(qual_D_adv~treatment+qual_D_adv.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$mean_median.L1)
reg3<-(felm(qual_D_adv~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg3.1<-(felm(qual_D_adv~treatment+qual_D_adv.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$symmetry.L1)
reg4<-(felm(qual_D_adv~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg4.1<-(felm(qual_D_adv~treatment+qual_D_adv.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$factor.L1)
reg5<-(felm(qual_D_adv~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg5.1<-(felm(qual_D_adv~treatment+qual_D_adv.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
stargazer(reg1,reg1.1, reg2, reg2.1,reg3,reg3.1, reg4,reg4.1, reg5, reg5.1, star.cutoffs = c(0.1, 0.05))

### Table A5
### candidate quality, measures of partisan bias based on presidential elections
state_elections_statehouse$treatment<-scale(state_elections_statehouse$EG_pres)
reg1<-(felm(qual_D_adv~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg1.1<-(felm(qual_D_adv~treatment+qual_D_adv.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$declination2_pres)
reg2<-(felm(qual_D_adv~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg2.1<-(felm(qual_D_adv~treatment+qual_D_adv.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$mean_median_pres)
reg3<-(felm(qual_D_adv~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg3.1<-(felm(qual_D_adv~treatment+qual_D_adv.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$symmetry_pres)
reg4<-(felm(qual_D_adv~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg4.1<-(felm(qual_D_adv~treatment+qual_D_adv.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$factor_pres)
reg5<-(felm(qual_D_adv~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg5.1<-(felm(qual_D_adv~treatment+qual_D_adv.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
stargazer(reg1,reg1.1, reg2, reg2.1,reg3,reg3.1, reg4,reg4.1, reg5, reg5.1, star.cutoffs = c(0.1, 0.05))



### Table 7
## fundraising, measures of partisan bias based on state legislative elections
####

state_elections_statehouse$treatment<-scale(state_elections_statehouse$EG.L1)
reg1<-(felm(total_receipts_perc_dem~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg1.1<-(felm(total_receipts_perc_dem~treatment+total_receipts_perc_dem.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$declination2.L1)
reg2<-(felm(total_receipts_perc_dem~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg2.1<-(felm(total_receipts_perc_dem~treatment+total_receipts_perc_dem.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$mean_median.L1)
reg3<-(felm(total_receipts_perc_dem~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg3.1<-(felm(total_receipts_perc_dem~treatment+total_receipts_perc_dem.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$symmetry.L1)
reg4<-(felm(total_receipts_perc_dem~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg4.1<-(felm(total_receipts_perc_dem~treatment+total_receipts_perc_dem.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$factor.L1)
reg5<-(felm(total_receipts_perc_dem~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg5.1<-(felm(total_receipts_perc_dem~treatment+total_receipts_perc_dem.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
stargazer(reg1,reg1.1, reg2, reg2.1,reg3,reg3.1, reg4,reg4.1, reg5, reg5.1, star.cutoffs = c(0.1, 0.05))

### Table A7
### fundraising, measures of partisan bias based on presidential elections
state_elections_statehouse$treatment<-scale(state_elections_statehouse$EG_pres)
reg1<-(felm(total_receipts_perc_dem~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg1.1<-(felm(total_receipts_perc_dem~treatment+total_receipts_perc_dem.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$declination2_pres)
reg2<-(felm(total_receipts_perc_dem~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg2.1<-(felm(total_receipts_perc_dem~treatment+total_receipts_perc_dem.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$mean_median_pres)
reg3<-(felm(total_receipts_perc_dem~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg3.1<-(felm(total_receipts_perc_dem~treatment+total_receipts_perc_dem.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$symmetry_pres)
reg4<-(felm(total_receipts_perc_dem~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg4.1<-(felm(total_receipts_perc_dem~treatment+total_receipts_perc_dem.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$factor_pres)
reg5<-(felm(total_receipts_perc_dem~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg5.1<-(felm(total_receipts_perc_dem~treatment+total_receipts_perc_dem.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
stargazer(reg1,reg1.1, reg2, reg2.1,reg3,reg3.1, reg4,reg4.1, reg5, reg5.1, star.cutoffs = c(0.1, 0.05))



### Table 8
## mean_voteshare, measures of partisan bias based on state legislative elections
state_elections_statehouse$treatment<-scale(state_elections_statehouse$EG.L1)
reg1<-(felm(mean_voteshare~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg1.1<-(felm(mean_voteshare~treatment+mean_voteshare.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$declination2.L1)
reg2<-(felm(mean_voteshare~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg2.1<-(felm(mean_voteshare~treatment+mean_voteshare.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$mean_median.L1)
reg3<-(felm(mean_voteshare~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg3.1<-(felm(mean_voteshare~treatment+mean_voteshare.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$symmetry.L1)
reg4<-(felm(mean_voteshare~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg4.1<-(felm(mean_voteshare~treatment+mean_voteshare.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$factor.L1)
reg5<-(felm(mean_voteshare~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg5.1<-(felm(mean_voteshare~treatment+mean_voteshare.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
stargazer(reg1,reg1.1, reg2, reg2.1,reg3,reg3.1, reg4,reg4.1, reg5, reg5.1, star.cutoffs = c(0.1, 0.05))

### Table A8
### mean_voteshare, measures of partisan bias based on presidential elections
state_elections_statehouse$treatment<-scale(state_elections_statehouse$EG_pres)
reg1<-(felm(mean_voteshare~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg1.1<-(felm(mean_voteshare~treatment+mean_voteshare.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$declination2_pres)
reg2<-(felm(mean_voteshare~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg2.1<-(felm(mean_voteshare~treatment+mean_voteshare.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$mean_median_pres)
reg3<-(felm(mean_voteshare~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg3.1<-(felm(mean_voteshare~treatment+mean_voteshare.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$symmetry_pres)
reg4<-(felm(mean_voteshare~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg4.1<-(felm(mean_voteshare~treatment+mean_voteshare.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
state_elections_statehouse$treatment<-scale(state_elections_statehouse$factor_pres)
reg5<-(felm(mean_voteshare~treatment|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
reg5.1<-(felm(mean_voteshare~treatment+mean_voteshare.L1|stateabrev+cycle|0|stateabrev, data=state_elections_statehouse[state_elections_statehouse$first_year==0,]))
stargazer(reg1,reg1.1, reg2, reg2.1,reg3,reg3.1, reg4,reg4.1, reg5, reg5.1, star.cutoffs = c(0.1, 0.05))

####
## The following code creates the plots for Figure 2
#####

## graph for fundraising
state_elections_statehouse$random<-rnorm(dim(state_elections_statehouse)[1])
width=.1
reg2.graph.outcome2<-(felm(total_receipts_perc_dem~random|stateabrev+cycle, data=state_elections_statehouse[!is.na(state_elections_statehouse$factor.L1),]))
reg2.graph.treatment2<-(felm(scale(factor.L1)~random|stateabrev+cycle, data=state_elections_statehouse[!is.na(state_elections_statehouse$total_receipts_perc_dem),]))
predictions.outcome2<-resid(reg2.graph.outcome2)
predictions.treatment2<-resid(reg2.graph.treatment2)
data.predictions2<-state_elections_statehouse[  !is.na(state_elections_statehouse$total_receipts_perc_dem)& !is.na(state_elections_statehouse$factor.L1),]
data.predictions2<-select(data.predictions2, stateabrev, cycle)
colnames( predictions.outcome2)<-"predictions.outcome"
colnames( predictions.treatment2)<-"predictions.treatment"
data.predictions2<-cbind( predictions.outcome2, predictions.treatment2)

data.predictions2<-as.data.frame(data.predictions2)
graph <- mutate(data.predictions2, bin=cut(predictions.treatment, breaks=seq(-2,2, width)))
mean_outcome<-summarise(graph, mean(predictions.outcome))
mean_outcome_raw<-summarise(graph, mean(predictions.outcome))

graph$predictions.outcome<-graph$predictions.outcome-mean(graph$predictions.outcome)
bin1.df <- data.frame(

bin.mean=tapply(graph$predictions.outcome,
                         graph$bin, mean, na.rm=TRUE),
                     mid=seq(-2+width/2 , 2-width/2 , width),
                     N=tapply(graph$predictions.outcome, graph$bin, length))
bin1.df$N_adjusted<-bin1.df$N*bin1.df$sample
library(ggplot2)
c <- ggplot(data=data.predictions2, aes(predictions.treatment, predictions.outcome))
c <- c + geom_point(data=bin1.df,aes(mid, bin.mean,size=N))
c <- c + stat_smooth(method = "lm")
c <- c + ylab("Residualized Democratic Share of Fundraising")
c <- c + xlab("Residualized, Lagged Democratic
 Advantage in Districting Process")
 c <- c + theme_bw() +theme(axis.title=element_text(size=14),axis.text=element_text(size=14))
#c
pdf(file="Figures/statehouse_total_receipts_perc_dem.pdf")
c
dev.off()
ggsave(file="Figures/statehouse_total_receipts_perc_dem.eps")
c
dev.off()


## graph for contestedness
state_elections_statehouse$random<-rnorm(dim(state_elections_statehouse)[1])
width=.1
reg2.graph.outcome2<-(felm(dem_unc_adv~random|stateabrev+cycle, data=state_elections_statehouse[!is.na(state_elections_statehouse$factor.L1),]))
reg2.graph.treatment2<-(felm(scale(factor.L1)~random|stateabrev+cycle, data=state_elections_statehouse[!is.na(state_elections_statehouse$dem_unc_adv),]))
predictions.outcome2<-resid(reg2.graph.outcome2)
predictions.treatment2<-resid(reg2.graph.treatment2)
data.predictions2<-state_elections_statehouse[  !is.na(state_elections_statehouse$dem_unc_adv)& !is.na(state_elections_statehouse$factor.L1),]
data.predictions2<-select(data.predictions2, stateabrev, cycle)
colnames( predictions.outcome2)<-"predictions.outcome"
colnames( predictions.treatment2)<-"predictions.treatment"
data.predictions2<-cbind( predictions.outcome2, predictions.treatment2)

data.predictions2<-as.data.frame(data.predictions2)
graph <- mutate(data.predictions2, bin=cut(predictions.treatment, breaks=seq(-2,2, width)))
mean_outcome<-summarise(graph, mean(predictions.outcome))
mean_outcome_raw<-summarise(graph, mean(predictions.outcome))

graph$predictions.outcome<-graph$predictions.outcome-mean(graph$predictions.outcome)
bin1.df <- data.frame(

bin.mean=tapply(graph$predictions.outcome,
                         graph$bin, mean, na.rm=TRUE),
                     mid=seq(-2+width/2 , 2-width/2 , width),
                     N=tapply(graph$predictions.outcome, graph$bin, length))
bin1.df$N_adjusted<-bin1.df$N*bin1.df$sample
library(ggplot2)
c <- ggplot(data=data.predictions2, aes(predictions.treatment, predictions.outcome))
c <- c + geom_point(data=bin1.df,aes(mid, bin.mean,size=N))
c <- c + stat_smooth(method = "lm")
c <- c + ylab("Residualized Democratic Share of Uncontested Seats")
c <- c + xlab("Residualized, Lagged Democratic
 Advantage in Districting Process")
 c <- c + theme_bw() +theme(axis.title=element_text(size=14),axis.text=element_text(size=14))
#c
pdf(file="Figures/statehouse_dem_unc_adv.pdf")
c
dev.off()
ggsave(file="Figures/statehouse_dem_unc_adv.eps")
c
dev.off()

## graph for quality
state_elections_statehouse$random<-rnorm(dim(state_elections_statehouse)[1])
width=.1
reg2.graph.outcome2<-(felm(qual_D_adv~random|stateabrev+cycle, data=state_elections_statehouse[!is.na(state_elections_statehouse$factor.L1),]))
reg2.graph.treatment2<-(felm(scale(factor.L1)~random|stateabrev+cycle, data=state_elections_statehouse[!is.na(state_elections_statehouse$qual_D_adv),]))
predictions.outcome2<-resid(reg2.graph.outcome2)
predictions.treatment2<-resid(reg2.graph.treatment2)
data.predictions2<-state_elections_statehouse[  !is.na(state_elections_statehouse$qual_D_adv)& !is.na(state_elections_statehouse$factor.L1),]
data.predictions2<-select(data.predictions2, stateabrev, cycle)
colnames( predictions.outcome2)<-"predictions.outcome"
colnames( predictions.treatment2)<-"predictions.treatment"
data.predictions2<-cbind( predictions.outcome2, predictions.treatment2)

data.predictions2<-as.data.frame(data.predictions2)
graph <- mutate(data.predictions2, bin=cut(predictions.treatment, breaks=seq(-2,2, width)))
mean_outcome<-summarise(graph, mean(predictions.outcome))
mean_outcome_raw<-summarise(graph, mean(predictions.outcome))

graph$predictions.outcome<-graph$predictions.outcome-mean(graph$predictions.outcome)
bin1.df <- data.frame(

bin.mean=tapply(graph$predictions.outcome,
                         graph$bin, mean, na.rm=TRUE),
                     mid=seq(-2+width/2 , 2-width/2 , width),
                     N=tapply(graph$predictions.outcome, graph$bin, length))
bin1.df$N_adjusted<-bin1.df$N*bin1.df$sample
library(ggplot2)
c <- ggplot(data=data.predictions2, aes(predictions.treatment, predictions.outcome))
c <- c + geom_point(data=bin1.df,aes(mid, bin.mean,size=N))
c <- c + stat_smooth(method = "lm")
c <- c + ylab("Residualized Democratic Advantage in Candidate Quality")
c <- c + xlab("Residualized, Lagged Democratic
 Advantage in Districting Process")
 c <- c + theme_bw() +theme(axis.title=element_text(size=14),axis.text=element_text(size=14))
#c
pdf(file="Figures/statehouse_qual_D_adv.pdf")
c
dev.off()
ggsave(file="Figures/statehouse_qual_D_adv.eps")
c
dev.off()


## graph for mean vote share
state_elections_statehouse$random<-rnorm(dim(state_elections_statehouse)[1])
width=.1
reg2.graph.outcome2<-(felm(mean_voteshare~random|stateabrev+cycle, data=state_elections_statehouse[!is.na(state_elections_statehouse$factor.L1),]))
reg2.graph.treatment2<-(felm(scale(factor.L1)~random|stateabrev+cycle, data=state_elections_statehouse[!is.na(state_elections_statehouse$mean_voteshare),]))
predictions.outcome2<-resid(reg2.graph.outcome2)
predictions.treatment2<-resid(reg2.graph.treatment2)
data.predictions2<-state_elections_statehouse[  !is.na(state_elections_statehouse$mean_voteshare)& !is.na(state_elections_statehouse$factor.L1),]
data.predictions2<-select(data.predictions2, stateabrev, cycle)
colnames( predictions.outcome2)<-"predictions.outcome"
colnames( predictions.treatment2)<-"predictions.treatment"
data.predictions2<-cbind( predictions.outcome2, predictions.treatment2)

data.predictions2<-as.data.frame(data.predictions2)
graph <- mutate(data.predictions2, bin=cut(predictions.treatment, breaks=seq(-2,2, width)))
mean_outcome<-summarise(graph, mean(predictions.outcome))
mean_outcome_raw<-summarise(graph, mean(predictions.outcome))

graph$predictions.outcome<-graph$predictions.outcome-mean(graph$predictions.outcome)
bin1.df <- data.frame(

bin.mean=tapply(graph$predictions.outcome,
                         graph$bin, mean, na.rm=TRUE),
                     mid=seq(-2+width/2 , 2-width/2 , width),
                     N=tapply(graph$predictions.outcome, graph$bin, length))
bin1.df$N_adjusted<-bin1.df$N*bin1.df$sample
library(ggplot2)
c <- ggplot(data=data.predictions2, aes(predictions.treatment, predictions.outcome))
c <- c + geom_point(data=bin1.df,aes(mid, bin.mean,size=N))
c <- c + stat_smooth(method = "lm")
c <- c + ylab("Residualized Democratic Mean Vote Share")
c <- c + xlab("Residualized, Lagged Democratic
 Advantage in Districting Process")
 c <- c + theme_bw() +theme(axis.title=element_text(size=14),axis.text=element_text(size=14))
#c
pdf(file="Figures/statehouse_meanvoteshare.pdf")
c
dev.off()
ggsave(file="Figures/statehouse_meanvoteshare.eps")
c
dev.off()
